package com.example.myapplication;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileSaveQQ {
    //存入存储文件
    public static boolean saveUserInfo(Context context,String account,String password){
        FileOutputStream fos = null;
        try{
            fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
            fos.write((account + ":" + password).getBytes());
            return true;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("openFileOutput时出错");
            return false;
        }
         catch (IOException e) {
             e.printStackTrace();
             System.out.println("write时出现问题");
            return false;
        }
    }
    //获取存储文件
    public static Map<String,String> getUserInfo(Context context) {
        String content = "";
        //输入流
        FileInputStream fis = null;
        try {
            fis = context.openFileInput("data.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            content = new String(buffer);
            Map<String, String> userMap = new HashMap<String, String>();
            String[] infos = content.split(":");
            userMap.put("account", infos[0]);
            userMap.put("password", infos[1]);
            return userMap;


        } catch (FileNotFoundException e) {
            System.out.println("openFileInput时出错");
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("available时出错");
            return null;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                System.out.println("关闭流时出错");
                e.printStackTrace();
            }
        }

    }


}
